import { Controller, Get, Post, Query, Render, Req } from '@nestjs/common';
import { MailService } from './mail/mail.service';

@Controller()
export class AppController {
  constructor(private readonly mailService: MailService) {
  }

  @Get()
  home() {
    return 'Hello 🍎🍐, 语雀 WebHook 已启动~';
  }

  @Post('yuque')
  async root(@Req() req: Request, @Query() query: any) {
    // @ts-ignore
    if (!req.body?.data) {
      return {
        message: 'request body is empty',
      };
    }

    // 获取指定发送的 email 地址
    const email = query['email'] || 'gengwenhao97@126.com';

    // @ts-ignore
    const yuqueData: any = { ...req.body }.data;
    const dict = {
      'update': '更新',
      'delete': '删除',
      'publish': '发布',
    };
    yuqueData.action_type_display = dict[yuqueData.action_type] || '未知';

    await this.mailService.notifyYuque(
      yuqueData,
      email,
    );

    return {
      message: 'success',
    };
  }
}
